package com.javadroid.service;

import java.util.Calendar;
import java.util.Random;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;


public class Trigger {
	
	public static int CODIGO_ALARM = 12344;
	
	public Trigger(Activity activity) {
		scheduleAlarm(activity);
	}
	
	private void scheduleAlarm(Activity activity){
		
		Intent intent = new Intent(activity, BootReceiver.class);
		PendingIntent pending = PendingIntent.getBroadcast(activity.getApplicationContext(), CODIGO_ALARM, intent, PendingIntent.FLAG_NO_CREATE);
		
		if(pending == null){
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeInMillis(System.currentTimeMillis());
			Random rand = new Random();
			int  Hora_Inicial_Randomica = rand.nextInt(23) + 1;
			
			calendar.set(Calendar.HOUR_OF_DAY, Hora_Inicial_Randomica);
			calendar.set(Calendar.MINUTE, 00);
			
			PendingIntent pendingIntent = PendingIntent.getBroadcast(activity.getApplicationContext(), CODIGO_ALARM, intent, 0);
			AlarmManager alarmManager = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE);
			alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,pendingIntent);
			Log.d("AlarmManager", "Alarm Criado com sucesso");
		}
	}
}
